#include "game_byte.h"
#include "game_log.h"
#include "game_version.h"

void __log(const char * format, ...){
  va_list args;
  va_start(args,format);

  vfprintf(stdout,format,args);

  va_end(args);
}


int main()
{
  __vprint();

  CGameByteArray bytes;
  bytes.setEndian(BIG_ENDIAN);
  bytes.writeUINT8(16);
  bytes.writeUINT8(0xff);
  bytes.writeUINT16(25535);
  bytes.writeUINT16(0xFFFF);
  bytes.writeUINT32(1000000);
  bytes.writeUINT32(0xFFFFFFF0);

  bytes.reset();


  __log("UINT8 :%d\n",bytes.readUINT8());
  __log("UINT8 :%d\n",bytes.readUINT8());
  __log("UINT16 :%d\n",bytes.readUINT16());
  __log("UINT16 :%d\n",bytes.readUINT16());
  __log("UINT32 :%d\n",bytes.readUINT32());
  __log("UINT32 :%ld\n",bytes.readUINT32());


  bytes.clear();

  bytes.writeINT8(-16);
  bytes.writeINT8(122);
  bytes.writeINT16(-15535);
  bytes.writeINT16(-155);
  bytes.writeINT16(3000);
  bytes.writeINT32(-1);
  bytes.writeINT32(-123455);
  bytes.writeINT32(12345);


  bytes.reset();


  __log("INT8 :%d\n",bytes.readINT8());
  __log("INT8 :%d\n",bytes.readINT8());
  __log("INT16 :%d\n",bytes.readINT16());
  __log("INT16 :%d\n",bytes.readINT16());
  __log("INT16 :%d\n",bytes.readINT16());
  __log("INT32 :%d\n",bytes.readINT32());
  __log("INT32 :%d\n",bytes.readINT32());
  __log("INT32 :%d\n",bytes.readINT32());

  // 继续读，应该踩assert
  // __log("INT32 :%d\n",bytes.readINT32());


  // BYTE cc[4] = {0};
  // bytes.readBytes(cc,4);
  // char str[128] = {0};
  // __bytes_to_hex(str,cc,4);
  // __log("djfksj %s",str);

  CGameByteStack s(64,10);

  // CGameByteStack_t* ctx = s.alloc();

  std::vector<CGameByteStack_t*> v;

  for(size_t i = 0;i < 6;i++){
    CGameByteStack_t* ctx = s.alloc();
    v.push_back(ctx);
    printf("1.alloc[%d]: %p\n",i,ctx->_ptr);
  }

  for(std::vector<CGameByteStack_t*>::iterator itr = v.begin();itr != v.end();itr++){
    CGameByteStack_t* ctx = (*itr);
    s.dealloc(ctx);
  }


  for(size_t i = 0;i < 10;i++){
    CGameByteStack_t* ctx = s.alloc();
    v.push_back(ctx);
    printf("2.alloc[%d]: %p\n",i,ctx->_ptr);
  }

  return 0;
}
